<!--vue模板   closeOnClickModal 设置为false ，点击遮罩时不会关闭dialog ，否则会关闭-->
<template>
  <el-dialog
    v-model="visible"
    :closeOnClickModal="false"
    width="35%"
    :title="title"
    @close="close"
  >
    <el-form label-width="120px">
      <el-form-item label="账号">
        <el-input
          v-model="user.account"
          :disabled="user.id != undefined"
          @blur="checkuser"
        />
      </el-form-item>

      <el-form-item label="密码" :class="{ hid: user.id != undefined }">
        <el-input v-model="user.password" type="password" />
      </el-form-item>

      <el-form-item label="生效日期">
        <el-date-picker
          v-model="user.effectiveDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择生效日期"
        />
      </el-form-item>

      <el-form-item label="是否锁定">
        <el-radio-group v-model="user.islock" class="ml-4">
          <el-radio :label="0" size="large">否</el-radio>
          <el-radio :label="1" size="large">是</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="签名">
        <el-upload
          class="avatar-uploader"
          :action="$host + '/upload/img'"
          name="myFile"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon">
            <Plus />
          </el-icon>
        </el-upload>
      </el-form-item>

      <el-form-item label="备注">
        <el-input
          v-model="user.comment"
          type="textarea"
          resize="none"
          :rows="4"
        />
      </el-form-item>

      <el-form-item label="角色">
        <el-select
          v-model="user.roleId"
          class="m-2"
          placeholder="请选择"
          size="large"
        >
          <el-option
            v-for="item in roleoptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>

    <div style="text-align: center">
      <el-button type="primary" @click="save">保存</el-button>
      <el-button @click="close">取消</el-button>
    </div>
  </el-dialog>
</template>

<!--vue 组件选项定义-->
<script>
import { Plus } from "@element-plus/icons-vue";
export default {
  data() {
    return {
      user: {
        account: "",
        password: "",
        effectiveDate: "",
        islock: 0,
        comment: "",
        signUrl: "",
      },
      imageUrl: "",
      isuserenable: "yes",
      roleoptions:[]
    };
  },
  computed: {
    title() {
      if (this.user.id) {
        return "修改用户";
      }
      return "添加用户";
    },
  },
  components: {
    Plus,
  },
  emits: ["update:visible", "refreshData"],
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
  },

  methods: {
    init(data) {
      if (data == undefined) {
        //说明用户执行的添加
        this.user = {
          account: "",
          password: "",
          effectiveDate: "",
          islock: 0,
          comment: "",
          signUrl: "",
        };
        this.imageUrl = "";
      } else {
        //修改
        this.user = data;
        this.user.islock = data.islock ? 1 : 0;
        //判断图片是否存在
        if (this.user.signUrl) {
          this.imageUrl = this.$host + this.user.signUrl;
        }
      }
      this.queryRole();
    },


    //查询角色
    queryRole(){
        this.$http.get("/api/role/all",{})
        .then(res=>{
            if(res.code==200){
              this.roleoptions=res.result;
            }
        })
    },

    //检查用户名是否可用
    checkuser() {
      if (this.user.account == "") {
        this.$message.warning("账号不能为空！");
        return;
      }

      //请求
      this.$http
        .get("/api/user/check", { username: this.user.account })
        .then((res) => {
          if (res.result != "yes") {
            //账号不可用
            this.isuserenable = "no";
            this.$message.error("用户名已占用！！");
          } else {
            //账号可用
            this.isuserenable = "yes";
          }
        });
    },
    save() {
      let promise;
      if (this.user.id) {
        //id存在 说明是修改
        promise = this.$http.put("/api/user/update", this.user);
      } else {
        //新增
        if (this.isuserenable == "no") {
          this.$message.error("用户名已占用！！");
          return;
        }
        promise = this.$http.post("/api/user/save", this.user);
      }
      promise.then((res) => {
        if (res.code == 200) {
          this.$message.success("保存成功");
          //关闭弹框
          this.close();

          //通知父组件刷新数据
          this.$emit("refreshData");
        }
      });
    },

    close() {
      this.$emit("update:visible", false);
    },

    //文件 上传成功
    handleAvatarSuccess(response, uploadFile) {
      if (response.code == 200) {
        this.imageUrl = URL.createObjectURL(uploadFile.raw);
        this.user.signUrl = response.result;
      } else this.imageUrl = "";
    },

    //文件上传之前
    beforeAvatarUpload(file) {
      const types = ["image/png", "image/jpeg"];
      if (types.indexOf(file.type) == -1) {
        this.$message.error("请选择jpg/png格式的图片");
        //停止 上传
        return false;
      } else if (file.size / 1024 / 1024 > 5) {
        this.$message.error("图片不能超过5M!");
        //停止 上传
        return false;
      }
      //继续上传
      return true;
    },
  },
};
</script>

<!--vue样式 scoped当前样式只适用于当前组件-->
<style scoped>
.hid {
  display: none;
}
.avatar-uploader .avatar {
  width: 120px;
  height: 120px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  text-align: center;
  border: 1px dashed var(--el-border-color);
}
</style>
